create or replace trigger trg_user
   before insert on users
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.user_id is null then
        loop
          select seq_user.nextval into id_val from dual;
          select count(1) into test_val from users where user_id=id_val;
          if (test_val=0) then
              :new.user_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
  end;
/
	
create or replace trigger trg_univ
   before insert on universities
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.univ_id is null then
        loop
          select seq_univ.nextval into id_val from dual;
          select count(1) into test_val from universities where univ_id=id_val;
          if (test_val=0) then
              :new.univ_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
    end;
/

create or replace trigger trg_fac
   before insert on faculties
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.faculty_id is null then
        loop
          select seq_fac.nextval into id_val from dual;
          select count(1) into test_val from faculties where faculty_id=id_val;
          if (test_val=0) then
              :new.faculty_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
    end;
/

create or replace trigger trg_subj
   before insert on subjects
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.subject_id is null then
        loop
          select seq_subj.nextval into id_val from dual;
          select count(1) into test_val from subjects where subject_id=id_val;
          if (test_val=0) then
              :new.subject_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
    end;
/

create or replace trigger trg_ex
   before insert on exams
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.exam_id is null then
        loop
          select seq_ex.nextval into id_val from dual;
          select count(1) into test_val from exams where exam_id=id_val;
          if (test_val=0) then
              :new.exam_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
    end;
/

create or replace trigger trg_mater
   before insert on materials
   for each row
   declare 
      test_val int;
      id_val int;
    begin
      if :new.material_id is null then
        loop
          select seq_mater.nextval into id_val from dual;
          select count(1) into test_val from materials where material_id=id_val;
          if (test_val=0) then
              :new.material_id:=id_val;
              exit;
          end if;
        end loop;       
      end if;
    end;
/